import requests

from decryption_data import *
import math


def header():
    headers = {
        'cp-os': 'android',
        'cp-ver': '1.1.45',
        'cp-uuid': '3969ee8a-6680-3223-8bd7-f48e43ea191b',
        'cp-abid': '3-100',
        'cp-channel': 'quick8',
        'cp-time': '1556425865',
        'cp-token': 'HXP~o53ET25vP64wEMziYxjnLQ4Afb4xb102WJslM64_',
        'cp-oem': 'Xiaomi',
        'cp-sver': '9',
        'cp-sign': 'd58ee706473b44ec3b1089fd75222b73',
        'cp-appid': '800',
        'Host': 'api.kuai8.mobi',
        'Accept-Encoding': 'gzip',
        'User-Agent': 'okhttp/3.3.1',
        'Connection': 'keep-alive'
    }
    return headers


postitions = []


def res_voice_list():
    smid = "ndghStN~uWRgv8TOWDCT9zvGAh2YEQDA"
    res_voice = requests.get(
        url='http://api.kuai8.mobi/1/voice/list.json?smid=%s&count=20&page=1' % (str(smid)),
        headers=header()
    )
    print(res_voice.json())
    total = res_voice.json()['result']['total']
    print(total)
    pages = total / 20
    pages = math.ceil(pages) + 1
    for page in range(1, int(pages)):
        res_voice = requests.get(
            url='http://api.kuai8.mobi/1/voice/list.json?smid={0}&count=20&page={1}'.format(smid, page),
            headers=header()
        )
        svid_list = res_voice.json()['result']['list']
        list_num = len(svid_list)
        for i in range(list_num):
            # svid = svid_acg_new(res_voice.json()['result']['list'][i]['svid'])
            svid = res_voice.json()['result']['list'][i]['svid']
            vid = svid_acg_new(svid)
            mid = smid_acg_new(res_voice.json()['result']['list'][i]['smid'])
            to_user = res_voice.json()['result']['list'][i]['to_user']
            to_svid = svid_acg_new(res_voice.json()['result']['list'][i]['to_svid'])
            # nick = res_voice.json()['result']['list'][i]['from_user']['nick']
            voice_amr = res_voice.json()['result']['list'][i]["voice_amr"]
            voice_second = res_voice.json()['result']['list'][i]["voice_second"]
            postition = {
                '评论排序': i + 1,
                '视频id': mid,
                # '用户昵称': nick,
                '语音时长': voice_second,
                '评论加密id': svid,
                '语音id': vid,
                '回复_用户id': to_user,
                '回复的vid': to_svid,
                '语音文件地址': voice_amr
            }

            if postition:
                for key in list(postition.keys()):
                    if not postition.get(key):
                        del postition[key]
                postitions.append(postition)

            else:
                print('postition的值是None')
    for i in postitions:
        print(i)


if __name__ == '__main__':
    res_voice_list()
